package com.dream.cgomall.util;

import com.google.gson.Gson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;

@Component
public class RedisUtil {
    @Autowired
    private  StringRedisTemplate redisTemplate;
    private  Gson gson = new Gson();



    /**
     * 存入redis
     * @param key key
     * @param object value
     * @param l 超时时间  (分钟)
     */
    public <T> void setRedis(String key,T object,Long l){
        redisTemplate.opsForValue().set(key,gson.toJson(object),l, TimeUnit.MINUTES);
    }

    public <T> void setRedis(String key,T object){
        String value = gson.toJson(object);
        redisTemplate.opsForValue().set(key,value,15, TimeUnit.MINUTES);
    }

    /**
     * 根据key 获取 value
     */
    public String getRedis(String key) {
        String first = redisTemplate.opsForValue().get(key);
        return first;
    }
}
